/* 建立資料庫表格範例
 
 //功能步驟：計算方法
 功能步驟:2 建立資料庫sqlite表格
 
 
 CREATE TABLE Defaults_Calculate_MACD_month (
 id INTEGER PRIMARY KEY AUTOINCREMENT ,
 code TEXT,
 Para_1 NUMERIC,
 Para_2 NUMERIC,
 Para_3 NUMERIC,
 Para_4 NUMERIC,
 Para_5 NUMERIC,
 winpercentage TEXT,
 yearreturn TEXT,
 count TEXT,
 startdate TEXT,
 enddate TEXT,
 id_class TEXT )

 
 
 */

//
//  DMPara.m
//  MacStockApp
//
//  Created by Andy MacPro on 12/8/6.
//
//

#import "DMPara.h"
#import "GBall.h"
#import "GBCalculationMethod_commonIndicator.h"
#import "GBCalculationMethod_selfIndicator.h"
#import "RLCalculationMethod.h"
@implementation DMPara
@synthesize para_dic;
@synthesize calculate_property_all;

- (id)init_code:(NSString *)object_code database_path:(NSString *)dpth _calculate_property_all:(NSMutableDictionary *)_calculate_property_all{
	
    if(self=[super init])
	{
        db_code=[object_code retain];
        databasePath_nsame=[dpth retain];
        [self setCalculate_property_all:_calculate_property_all];
               
    }
	
	return self;
}

#pragma mark -
#pragma mark 輸入計算方法 取出參數值
-(NSMutableDictionary *) Para_calculate_method:(NSString *)calculate_method period:(NSString *)dwm{
    
    //1.建立主參數記憶體
    if (!para_dic) {
        //NSLog(@"建立主參數記憶體");
        NSMutableDictionary *para_dic_temp= [[NSMutableDictionary alloc] init]  ;
        [para_dic_temp setObject:db_code forKey:@"code"];
        
        [self setPara_dic:para_dic_temp];
        [para_dic_temp release];
        
    }
       
    //2.計算方法dic
    NSMutableDictionary *para_calculate_method;
    
    if ( ![para_dic objectForKey:  calculate_method] ) {
        //如果沒有此計算方法的參數 就新增此方法參數
        //NSLog(@"如果沒有此計算方法的參數 就新增此方法參數");
        para_calculate_method= [[NSMutableDictionary alloc] init]  ;
        [para_dic setObject:para_calculate_method forKey:calculate_method];
        [para_calculate_method release];
             
    }
    else{
        para_calculate_method=[para_dic objectForKey:  calculate_method];
    }
    
    //3.建立字典方法->區間
    
    NSMutableDictionary *para_dic_period;
    if (![para_calculate_method objectForKey:dwm]) {
        //沒有此區間的字典變數
        //NSLog(@"沒有此區間的字典變數");
        para_dic_period=[[NSMutableDictionary alloc] init];
        [para_calculate_method setObject:para_dic_period forKey:dwm];
        [para_dic_period release];
    
    }
    else{
        para_dic_period=[para_calculate_method objectForKey:dwm];
    }
    
    /*
    //沒有此區間的字典變數
    para_dic_period=[[NSMutableDictionary alloc] init];
    [para_value_key setObject:para_dic_period forKey:dwm];
    [para_dic_period release];
    */
    
    
    //1.參數值宣告
    float para_value_1;
    float para_value_2;
    float para_value_3;
    float para_value_4;
    float para_value_5;
    
    float long_cost;
    float short_cost;
    float stop_ratio;
    int   is_stop;
    
    //2.先設定五個參數預設值
    {
        para_value_1=0;
        para_value_2=0;
        para_value_3=0;
        para_value_4=0;
        para_value_5=0;
        
        long_cost=0;
        short_cost=0;
        stop_ratio=0;
        is_stop=0;
        
    }
    
    //3.開啓資料庫 取出參數資料
    if (![para_dic_period objectForKey:Para_1] || ![para_dic_period objectForKey:Para_2] || ![para_dic_period objectForKey:Para_3] || ![para_dic_period objectForKey:Para_4] || ![para_dic_period objectForKey:Para_5]){
       // NSLog(@"沒有資料 所以進入資料庫去取資料");
        
        FMDatabase *db=[FMDatabase databaseWithPath:databasePath_nsame];
        
        //進行資料庫處理
        //記住一定要 開啓資料庫 [db open]        
        if (![db open]) {
            
           // NSLog(@"無法開啓資料庫");
            
        }
        else{
            //開啓資料庫
            
            NSString *sels=[NSString stringWithFormat:@"SELECT * FROM %@_%@ WHERE code='%@'",calculate_method,dwm,db_code];
            
            //NSLog(@"sels=%@",sels);
            
            FMResultSet *rs=[db executeQuery:sels];
            
            BOOL isHave_para;
            isHave_para=NO;
            
            while ([rs next]) {
                isHave_para=YES;//檢查資料庫內是否可以找到該參數
                para_value_1=[[rs stringForColumn:Para_1] floatValue];
                para_value_2=[[rs stringForColumn:Para_2] floatValue];
                para_value_3=[[rs stringForColumn:Para_3] floatValue];
                para_value_4=[[rs stringForColumn:Para_4] floatValue];
                para_value_5=[[rs stringForColumn:Para_5] floatValue];
                
                
                long_cost=[[rs stringForColumn: Para_long_cost] floatValue];
                short_cost=[[rs stringForColumn:Para_short_cost] floatValue];
                stop_ratio=[[rs stringForColumn:Para_stop_ratio] floatValue];
                is_stop=[[rs stringForColumn:Para_is_stop] floatValue];
                
            }
            //4.存入該區間內的字典
            {
                //清除
                [para_dic_period removeObjectForKey:Para_1];
                [para_dic_period removeObjectForKey:Para_2];
                [para_dic_period removeObjectForKey:Para_3];
                [para_dic_period removeObjectForKey:Para_4];
                [para_dic_period removeObjectForKey:Para_5];
                
                [para_dic_period removeObjectForKey:Para_Optimize_long_cost];
                [para_dic_period removeObjectForKey:Para_Optimize_short_cost];
                [para_dic_period removeObjectForKey:Para_Optimize_Stop_ratio];
                [para_dic_period removeObjectForKey:Para_Optimize_isUse_Stop];
                
                //存入記憶體內
                if ( isHave_para ) {
                    //NSLog(@"表示有資料庫內找得到參數值");
                    //表示有資料庫內找得到參數值
                     //將參數值存入區間內的字典
                    [para_dic_period setObject:[NSNumber numberWithFloat:para_value_1] forKey:Para_1];
                    [para_dic_period setObject:[NSNumber numberWithFloat:para_value_2] forKey:Para_2];
                    [para_dic_period setObject:[NSNumber numberWithFloat:para_value_3] forKey:Para_3];
                    [para_dic_period setObject:[NSNumber numberWithFloat:para_value_4] forKey:Para_4];
                    [para_dic_period setObject:[NSNumber numberWithFloat:para_value_5] forKey:Para_5];
                    
                    [para_dic_period setObject:[NSNumber numberWithFloat:long_cost] forKey:Para_Optimize_long_cost];
                    [para_dic_period setObject:[NSNumber numberWithFloat:short_cost] forKey:Para_Optimize_short_cost];
                    [para_dic_period setObject:[NSNumber numberWithFloat:stop_ratio] forKey:Para_Optimize_Stop_ratio];
                    [para_dic_period setObject:[NSNumber numberWithInt: is_stop] forKey:Para_Optimize_isUse_Stop];
                    
                }
                else{
                    //NSLog(@"表示有資料庫找不到參數值 採用預設值");
                    
                    
                    //取出預設值
                   
                    NSMutableDictionary *calculate_property_method;
                    {
                        //a.所有計算方法屬性
                        if (![self calculate_property_all]) {
                           [self setCalculate_property_all:[RLCalculationMethod calculate_method_property]];
                        }
                        
                       
                        //b.取出該計算方法屬性
                        calculate_property_method=[calculate_property_all objectForKey:calculate_method];
                    }
                  
                    float default1=[[calculate_property_method objectForKey:Para_Default_value_1 ] floatValue] ;
                    float default2=[[calculate_property_method objectForKey:Para_Default_value_2 ] floatValue] ;
                    float default3=[[calculate_property_method objectForKey:Para_Default_value_3 ] floatValue] ;
                    float default4=[[calculate_property_method objectForKey:Para_Default_value_4 ] floatValue] ;
                    float default5=[[calculate_property_method objectForKey:Para_Default_value_5 ] floatValue] ;
                   
                    float default_long_cost=[[calculate_property_method objectForKey:Para_Optimize_long_cost ] floatValue] ;
                    float default_short_cost=[[calculate_property_method objectForKey:Para_Optimize_short_cost ] floatValue] ;
                    float default_stop_ratio=[[calculate_property_method objectForKey:Para_Optimize_Stop_ratio ] floatValue] ;
                    int default_is_stop=[[calculate_property_method objectForKey:Para_Optimize_isUse_Stop ] intValue] ;
                     
                    
                    
                    [para_dic_period setObject:[NSNumber numberWithFloat:default1] forKey:Para_1];
                    [para_dic_period setObject:[NSNumber numberWithFloat:default2] forKey:Para_2];
                    [para_dic_period setObject:[NSNumber numberWithFloat:default3] forKey:Para_3];
                    [para_dic_period setObject:[NSNumber numberWithFloat:default4] forKey:Para_4];
                    [para_dic_period setObject:[NSNumber numberWithFloat:default5] forKey:Para_5];
                
                    
                    [para_dic_period setObject:[NSNumber numberWithFloat:default_long_cost] forKey:Para_Optimize_long_cost];
                    [para_dic_period setObject:[NSNumber numberWithFloat:default_short_cost] forKey:Para_Optimize_short_cost];
                    [para_dic_period setObject:[NSNumber numberWithFloat:default_stop_ratio] forKey:Para_Optimize_Stop_ratio];
                    [para_dic_period setObject:[NSNumber numberWithInt: default_is_stop] forKey:Para_Optimize_isUse_Stop];
                    
                
                }
                
                
                
            }
            
           
            
        }
        
        //關閉資料庫
        [db close];
        
    }
    else{
    
       
    }
    
    
    return para_dic;
}
 
#pragma mark -
#pragma mark 儲存修改的參數值

-(BOOL)Save_Para:(NSString *)calculate_method Para_dic:(NSMutableDictionary *)par_dic period:(NSString *)dwm{
    
    NSLog(@"Save_Para calculate_method:%@",calculate_method);
    NSLog(@"Save_Para par_dic:%@",par_dic);
    NSLog(@"Save_Para dwm:%@",dwm);
    
    //1.資料庫物件宣告
    FMDatabase *db;
    FMResultSet *rs;
    
    //2.開啓資料庫
    db=[FMDatabase databaseWithPath:databasePath_nsame];    
   
    //3.檢查資料庫內是否有資料
    BOOL isHave_para;    
    {
        //記住一定要 開啓資料庫 [db open]
        if (![db open]) {
            
            NSLog(@"無法開啓資料庫 =%@",databasePath_nsame);
            return NO;
        }
        else{
            //a.
            NSString *sql_check=[NSString stringWithFormat:@"SELECT * FROM %@_%@ WHERE code='%@'",calculate_method,dwm,db_code];
            
            
            //b.
            rs=[db executeQuery:sql_check];
            
            //c.
            isHave_para=NO;
            while ([rs next]) {
                isHave_para=YES;//檢查資料庫內是否可以找到該參數
            }
            
           
        }
       
    }
        
    //4.插入或是更新
    {
        //技術指標參數
        float p1,p2,p3,p4,p5;
        float long_cost,short_cost;//買進與賣出交易成本
        float stop_ratio;//停損率
        int is_stop;//是否有停損機制
        
        //取出資料
        {
            p1=[[par_dic objectForKey:Para_1 ] floatValue];
            p2=[[par_dic objectForKey:Para_2 ] floatValue];
            p3=[[par_dic objectForKey:Para_3 ] floatValue];
            p4=[[par_dic objectForKey:Para_4 ] floatValue];
            p5=[[par_dic objectForKey:Para_5 ] floatValue];
            
            if ([par_dic objectForKey:Para_Optimize_long_cost]) {
                long_cost= [[par_dic objectForKey:Para_Optimize_long_cost] floatValue];
            }
            else{
                long_cost=0;
            }
            
            if ([par_dic objectForKey:Para_Optimize_short_cost]) {
                short_cost= [[par_dic objectForKey:Para_Optimize_short_cost] floatValue];
            }
            else{
                short_cost= 0;
            }
            
            if ([par_dic objectForKey:Para_Optimize_isUse_Stop]) {
                is_stop= [[par_dic objectForKey:Para_Optimize_isUse_Stop] intValue];
            }
            else{
                is_stop= 0;
            }
            
            if ([par_dic objectForKey:Para_Optimize_Stop_ratio]) {
                stop_ratio= [[par_dic objectForKey:Para_Optimize_Stop_ratio] floatValue];
            }
            else{
                stop_ratio= 0;
            }
                 
        
        }
                      
        
        
        
        if (isHave_para==YES) {
            //NSLog(@"更新 模式");
            
            NSString *sql_update=[NSString stringWithFormat:
                                  @"UPDATE %@_%@ SET Para_1 = '%g' , Para_2 = '%g' , Para_3 = '%g' , Para_4 = '%g' , Para_5 = '%g' , Para_long_cost = '%g' , Para_short_cost = '%g' , Para_is_stop = '%i', Para_stop_ratio = '%g' WHERE code = '%@'  ",calculate_method,dwm,p1,p2,p3,p4,p5,long_cost,short_cost,is_stop,stop_ratio,db_code  ];
            
            //NSLog(@"sql_update=%@",sql_update );
            
            
            
            if( [db executeUpdate:sql_update,nil]){
              //  NSLog(@"更新成功");
            }
            else{
                //NSLog(@"更新失敗");
            }
            
        }
        else{
            //NSLog(@"插入 模式");
            
            
            
            NSString *sql_insert_into=[NSString stringWithFormat:
                                       @"INSERT INTO %@_%@ (code,Para_1,Para_2,Para_3,Para_4,Para_5,Para_long_cost,Para_short_cost,Para_is_stop,Para_stop_ratio ) VALUES ('%@','%g','%g','%g','%g','%g','%g','%g','%i','%g')",calculate_method,dwm,db_code,p1,p2,p3,p4  ,p5,long_cost,short_cost,is_stop,stop_ratio];
            
            //NSLog(@"sql_insert_into=%@",sql_insert_into );
            
            
            
            if( [db executeUpdate:sql_insert_into,nil]){
                //NSLog(@"插入成功");
            }
            else{
                //NSLog(@"插入失敗");
            }
            
        }
        
        //關閉資料庫
        [db close];
    }
       
    
    //5.記憶體更新
    {
        //取出存放參數的位置
        NSMutableDictionary *get_para_dic=[[[self para_dic] objectForKey:calculate_method] objectForKey:dwm];
        
        //如果可以找到位置
        if (get_para_dic) {
           
            [get_para_dic removeObjectForKey:Para_1];
            [get_para_dic removeObjectForKey:Para_2];
            [get_para_dic removeObjectForKey:Para_3];
            [get_para_dic removeObjectForKey:Para_4];
            [get_para_dic removeObjectForKey:Para_5];
            
            [get_para_dic setObject:[par_dic objectForKey:Para_1 ]  forKey:Para_1];
            [get_para_dic setObject:[par_dic objectForKey:Para_2 ]  forKey:Para_2];
            [get_para_dic setObject:[par_dic objectForKey:Para_3 ]  forKey:Para_3];
            [get_para_dic setObject:[par_dic objectForKey:Para_4 ]  forKey:Para_4];
            [get_para_dic setObject:[par_dic objectForKey:Para_5 ]  forKey:Para_5];
            
            
            [get_para_dic removeObjectForKey:Para_Optimize_long_cost];
            [get_para_dic removeObjectForKey:Para_Optimize_short_cost];
            [get_para_dic removeObjectForKey:Para_Optimize_isUse_Stop];
            [get_para_dic removeObjectForKey:Para_Optimize_Stop_ratio];
            
            [get_para_dic setObject:[par_dic objectForKey:Para_Optimize_long_cost ]  forKey:Para_Optimize_long_cost];
            [get_para_dic setObject:[par_dic objectForKey:Para_Optimize_short_cost ]  forKey:Para_Optimize_short_cost];
            [get_para_dic setObject:[par_dic objectForKey:Para_Optimize_isUse_Stop ]  forKey:Para_Optimize_isUse_Stop];
            [get_para_dic setObject:[par_dic objectForKey:Para_Optimize_Stop_ratio ]  forKey:Para_Optimize_Stop_ratio];
            
            
            
             return YES;            
        }
        else{
            return NO;
        }
        
    }
    
   

}

#pragma mark -
#pragma mark 物件消滅
-(void)dealloc{
    
    [db_code release];
    [databasePath_nsame release];
    
    if (para_dic) {
        [para_dic release];
    }
    
        
    if (para_dic_mesa) {
        [para_dic_mesa release];
    }
    if (para_dic_lf) {
        [para_dic_lf release];
    }
    if (para_dic_kd) {
        [para_dic_kd release];
    }
    if (para_dic_macd) {
        [para_dic_macd release];
    }
    if (para_dic_ma) {
        [para_dic_ma release];
    }
    
    [super dealloc];
    
}



@end
